package com.jin.thread;

import java.util.concurrent.locks.LockSupport;

public class Demo01 {
    public static void main(String[] args) {
        printNum printNum1 = new printNum();
        printNum printNum2 = new printNum();
        new Thread(printNum1::oddNumber).start();
        new Thread(printNum1::evenNumber).start();
        LockSupport.park();

    }
}

class printNum{

    public synchronized void oddNumber() {
        for (int i = 1; i < 10; i += 2) {
            System.out.println("奇数："+ i);
            this.notify();
            try {
                this.wait();
//                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
    }

    public synchronized void evenNumber() {
        for (int i = 2; i <= 10; i += 2) {
            this.notify();
            System.out.println("偶数：" + i);
            try {
                this.wait();
//                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
    }


}

class singleton{
    private static volatile Demo01 demo01 = null;

    private singleton(){

    }

    public static Demo01 getSingleton(){
        if (demo01==null){
            demo01 = new Demo01();
        }
        return demo01;
    }
}
